/**
 * Created by Administrator on 2018/1/8.
 */
var listUser;//该页所有用户信息
var ListMax;//最大页
var ListNumber;//该页用户数量
var ListPage;//该页页数

var prePage;//上一页
var nextPage;//下一页
var isFirstPage;//是否为首页
var isLastPage;//是否为末页
var hasPreviousPage;//有上一页
var hasPreviousPage;//有下一页

var index = 1;


$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "/score/scoreList.do",
        async: false,
        success: function(date) {
            //获取数据
            listUser = date.data.list;
            ListMax = date.data.pages;
            ListNumber = listUser.length;
            ListPage = date.data.pageNum;


            console.log(listUser);
            console.log(ListMax);
            console.log(ListPage);


        },
        error: function(date) {
            alert("服务器繁忙!");
        }
    });

    //初始化界面
    document.getElementById("pageNow").innerHTML = "第"+ListPage+"页";
    document.getElementById("pageTotal").innerHTML = "共"+ListMax+"页";

    //初始化列表
    for(var i = 0;i<listUser.length;i++){
        initListItem(i+1,listUser[i].username,listUser[i].score);
    }

});

function initListItem(i,Susername,SuserScore){
    var father = document.getElementById("list");
    var ListItem = document.createElement("div");
    var ID = document.createElement("div");
    var username = document.createElement("div");
    var userScore = document.createElement("div");
    ListItem.className = "listItem";
    ID.innerHTML =i+".";
    ID.className = "listIndex";
    username.innerHTML = Susername;
    username.className = "listUsername";
    userScore.innerHTML =SuserScore;
    userScore.className ="listScore";
    ListItem.appendChild(ID);
    ListItem.appendChild(username);
    ListItem.appendChild(userScore);
    father.appendChild(ListItem);
}

function firstPage() {
    if (ListPage == 1) {
        alert("当前已经是首页");
        return;
    }
    selectByPage(1);

}

function lastPage(){
    if (ListPage == ListMax) {
        alert("当前已经是末页");
        return;
    }
    selectByPage(ListMax);
}

function beforPage(){
    if (ListPage == 1) {
        alert("当前已经是首页");
        return;
    }
    selectByPage(ListPage-1);
}

function nextPage(){
    if (ListPage == ListMax) {
        alert("当前已经是末页");
        return;
    }
    selectByPage(ListPage+1);
}

function selectByPage(page){
    $.ajax({
        type: "POST",
        url: "/score/scoreList.do",
        async: false,
        data: {"pageNum":page},
        success: function(date) {
            //获取数据
            listUser = date.data.list;
            ListMax = date.data.pages;
            ListNumber = listUser.length;
            ListPage = date.data.pageNum;


            console.log(listUser);
            console.log(ListMax);
            console.log(ListPage);


        },
        error: function(date) {
            alert("服务器繁忙!");
        }
    });

    //清空子节点
    var fathe = document.getElementById("list");
    var childs = fathe.childNodes;
    console.log(childs);
    for(var i = childs.length-1;i>=0;i--){
        fathe.removeChild(childs[i]);
    }

    //初始化界面
    document.getElementById("pageNow").innerHTML = "第"+ListPage+"页";
    document.getElementById("pageTotal").innerHTML = "共"+ListMax+"页";

    //初始化列表
    for(var i = 0;i<listUser.length;i++){
        initListItem(i+1,listUser[i].username,listUser[i].score);
    }

}


